from django.shortcuts import render
from rest_framework.generics import ListAPIView
from .models import CourseCategory,Course
from .serializers import CourseCategoryModelSerializer,CourseModelSerializer
# Create your views here.
class CourseCategoryListAPIView(ListAPIView):
    queryset = CourseCategory.objects.filter(is_show=True,is_delete=False).order_by('orders')
    serializer_class = CourseCategoryModelSerializer


from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from .paginations import CustomPageNumberPagination
class CourseAPIView(ListAPIView):
    queryset = Course.objects.filter(is_delete=False,is_show=True).order_by('orders')
    serializer_class = CourseModelSerializer
    filter_backends = [DjangoFilterBackend,OrderingFilter]
    filter_fields=('course_category',)#http://api.luffycity.cn:8000/courses/?course_category=2 筛选出course_category=2的对象
    #上设置价格排序
    ordering_fields=('id','students','price')
    #指定分页器
    pagination_class = CustomPageNumberPagination


from .serializers import CourseDetailModelSerializer
from rest_framework.generics import RetrieveAPIView
class CourseRetrieveAPIView(RetrieveAPIView):
    queryset = Course.objects.filter(is_show=True,is_delete=False)
    serializer_class = CourseDetailModelSerializer

from rest_framework.permissions import IsAuthenticated
from django.conf import settings
from rest_framework.views import APIView
from luffyapi.libs.polyv import PolyvPlayer
from rest_framework.response import Response
class PolyvAPIView(APIView):
    #保利威视屏token的接口
    permission_classes = [IsAuthenticated]
    def get(self, request):
        """获取保利威云视频加密播放的token"""
        """接受客户端的请求参数"""
        vid = request.query_params.get("vid")  # 视频播放ID
        remote_addr = request.META.get("REMOTE_ADDR")  # 用户的IP
        user_id = request.user.id      # 用户ID
        user_name = request.user.username  # 用户名
        polyv = PolyvPlayer(
            settings.POLYV_CONFIG["userId"],
            settings.POLYV_CONFIG["secretkey"],
            settings.POLYV_CONFIG["tokenUrl"],
        )

        data = polyv.get_video_token(vid,remote_addr,user_id,user_name)

        return Response(data)